#!/bin/sh
DIRNAME=`dirname $0`
#PATH_TO_ME=`which $0`;
# Give a space-separated list of classpath items RELATIVE TO THE CURRENT SCRIPT
# These will be resolved into absolute pathnames
# Wildcards are allowed
CLASSPATH_RELATIVE=owltools-runner-all.jar

# If literal classpath values are needed, uncomment the line below
# This can be useful if the classpath contains URLs
# CLASSPATH_LITERAL=""

# To set a specific default Java path, set the JAVAPATH variable below.
# This value can be overridden with the -Jvm= option.
# If JAVAPATH is not set, the script will use whatever version of Java is on the
# path. If there is no copy of Java on the path, the JAVA_HOME environment
# variable will be used. If that fails, we just use "java" in the hopes that the
# failure message will make a little more sense.
# JAVAPATH="java"

PATH_SEP=":"

JAVAARGS=" "
CMDARGS=" "

# Remove the name of this script from the end of the path
PATH_TO_ME=`which $0`;
PATH_TO_ME=`echo $PATH_TO_ME | sed -e "s/\(.*\)\/.*/\1/g"`

# Just the name of the script.
SCRIPTNAME=`echo $PATH_TO_ME | sed -e "s/.*\/\(.*\)/\1/g"`

## Add vmoptions to JAVAARGS if the proper file is available.
if [ -e "$PATH_TO_ME/$SCRIPTNAME.vmoptions" ]
then
    VMOPTIONS=`cat $PATH_TO_ME/$SCRIPTNAME.vmoptions`
    for OPTION in "$VMOPTIONS"
    do
	JAVAARGS="$JAVAARGS '${OPTION}'"
    done
else
    if [ $OWLTOOLS_MEMORY ]
    then
        JAVAARGS="$JAVAARGS -Xmx$OWLTOOLS_MEMORY"
    else
        JAVAARGS="$JAVAARGS -Xmx4G"
    fi
fi

## Walk through all the command line arguments and add them to
## the CMDARGS depending.

for ARG in "$@"
do
    CMDARGS="${CMDARGS} '${ARG}'"
    shift 1;
done

## Add to CLASSPATH using CLASSPATH_RELATIVE.
CLASSPATH=""
for ARG in "$CLASSPATH_RELATIVE"
do
    DEREFERENCED_CLASSPATH=`ls -1 -L $PATH_TO_ME/$ARG | grep -v ontologyrelease`
    for CP_ENTRY in $DEREFERENCED_CLASSPATH
    do
	if [ -z "$CLASSPATH" ]
	then
	    CLASSPATH="$CP_ENTRY"
	else
	    CLASSPATH="$CLASSPATH$PATH_SEP$CP_ENTRY"
	fi
    done
done

## Add to CLASSPATH using CLASSPATH_LITERAL.
if [ -n "$CLASSPATH_LITERAL" ]
then
    for CP_ENTRY in $CLASSPATH_LITERAL
    do
	if [ -z "$CLASSPATH" ]
	then
	    CLASSPATH="$CP_ENTRY"
	else
	    CLASSPATH="$CLASSPATH$PATH_SEP$CP_ENTRY"
	fi
    done
fi

## Figure out which java to use.
if [ -z "$JAVAPATH" ]
then
    JAVAPATH=`which java`
    if [ -z "$JAVAPATH" ]
    then
	if [ -n "$JAVA_HOME" && -e "$JAVA_HOME" ]
	then
	    JAVAPATH=$JAVA_HOME/bin/java
	else
	    JAVAPATH="java"
	fi
    fi
fi

## Assemble and run the final command.
CMD="$JAVAPATH -Xms2048M -DentityExpansionLimit=2048000 -Djava.awt.headless=true -Djava.library.path=$PATH_TO_ME/../lib -classpath $CLASSPATH -DlauncherDir=$PATH_TO_ME $JAVAARGS owltools.cli.CommandLineInterface $CMDARGS"

## DEBUG
## Let's see a little of the environment if we declare DEBUG=1.
if [ $DEBUG ]
then
    for MYVAR in DEBUG PATH_TO_ME SCRIPTNAME DIRNAME JAVAARGS CMDARGS CLASSPATH_RELATIVE CMD
    do
	LETVAR=`echo "$"$MYVAR`
	TMPVAR=`eval echo $LETVAR`
	echo "${MYVAR}: \"${TMPVAR}\""
    done
fi

## Run the final command.
if [ $DEBUG ]
then
    echo "$CMD"
fi
sh -c "$CMD"
